package cn.glenn._04weibo.controller;


import cn.glenn._04weibo.base.response.JsonResult;
import cn.glenn._04weibo.mapper.UserMapper;
import cn.glenn._04weibo.pojo.dto.UserLoginParam;
import cn.glenn._04weibo.pojo.dto.UserRegParam;
import cn.glenn._04weibo.pojo.enity.User;
import cn.glenn._04weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/")
@Slf4j
@Tag(name="01-用户管理模块",description = "01-用户管理模块",extensions ={
        @Extension(properties = {@ExtensionProperty(name = "x-order", value = "1", parseValue = true)}
        )})
public class UserController {
    @Autowired
    private UserMapper userMapper;


    @PostMapping("reg")
    @Operation(summary = "用户注册")
    @Parameters({
            @Parameter(name = "username", description = "用户名",required = true),
            @Parameter(name = "password", description = "用户密码",required = true),
            @Parameter(name = "nickname", description = "昵称",required = true)
    })
    @ApiOperationSupport(order = 1)
    public JsonResult userReg(@Validated @RequestBody UserRegParam param) {
        log.debug("userReg param:{}", param);
        if((userMapper.checkUsername(param.getUsername())==1)){
            return JsonResult.usernameExist();}
        User user = new User();
        BeanUtils.copyProperties(param,user);
        user.setCreateTime(new Date());
        int result = userMapper.insertUser(user);
        if (result == 1) {return JsonResult.success();}
        else {return JsonResult.fail();}
    }

    @PostMapping("login")
    @Operation(summary = "用户登录")
    @Parameters({
            @Parameter(name = "username",description = "用户名",required = true),
            @Parameter(name = "password",description = "用户密码",required = true)
    })
    @ApiOperationSupport(order = 2)
    public JsonResult userLogin(@RequestBody UserLoginParam param,HttpSession session) {
        UserVO userVO = userMapper.checkUsernameAndPassword(param);
        if ((userVO!=null)){
            log.debug("userLogin userVO:{}", userVO);
            session.setAttribute("user",userVO);
            /*id = userVO.getId();
            username = param.getUsername();*/
            return JsonResult.success();}
        else {return JsonResult.fail();}
    }

    @GetMapping("currentUser")
    @Operation(summary = "获取当前用户")
    @ApiOperationSupport(order = 3)
    public JsonResult currentUSer(HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.success(userVO);
    }

    @GetMapping("logout")
    @Operation(summary = "退出登录")
    @ApiOperationSupport(order = 4)
    public void logout(HttpSession session) {
        session.removeAttribute("user");
    }
}
